args Controls CreativityMeasure version

local lab CreateOccWages_`version'
preserve

* setup
local ipw=1

* setup
replace Creativity=creativityt_`CreativityMeasure'_7



local lwage   lwage_I33  lwage_I42  lwage_I46  lwage_I50  lwage_I55
local lpay    lpay_I33   lpay_I42   lpay_I46   lpay_I50   lpay_I55  
local lhours  lhours_I33 lhours_I42 lhours_I46 lhours_I50 lhours_I55  

local O  lwage lpay lhours

* calculate ipw weights (including self employed)
foreach o of local O {

	foreach y of local `o' {
		if `ipw'==1{
			ge io=`y'!=.
			qui logit io $HDC_0
			drop io
			predict p_`y', pr
		}

		if `ipw'==0{
			ge  p_`y'=1
		}
	
	}	
}


* reshape to long dataset
reshape long lwage_I lpay_I lhours_I p_lwage_I p_lpay_I p_lhours_I  `version'CreatOcc_I SE_I, i(NCDSID) j(age)
su p_*

ge iCreativity = Creativity * `version'CreatOcc
ge iCogAbility = CogAbility * `version'CreatOcc


* All
foreach o of local O {
	rifhdreg `o'  `Controls' iCreativity iCogAbility `version'CreatOcc  [pweight=1/p_`o'], rif(mean) cluster(NCDSID)
	
	mat b = e(b)
	mat b = b'
	svmat b
	qui su b1 if b1!=0
	mat define p= r(N)
	drop b1
	qui estadd scalar Par=p[1,1]
	qui estadd scalar Ni=e(N_clust) 
	qui estadd local mdv "-"
	est sto es_`o'_All
}

* Employees
foreach o of local O {
	rifhdreg `o'  `Controls' iCreativity iCogAbility `version'CreatOcc  [pweight=1/p_`o'] if SE==0, rif(mean) cluster(NCDSID)
	
	mat b = e(b)
	mat b = b'
	svmat b
	qui su b1 if b1!=0
	mat define p= r(N)
	drop b1
	qui estadd scalar Par=p[1,1]
	qui estadd scalar Ni=e(N_clust) 
	qui estadd local mdv "-"
	est sto es_`o'_Emp
}


* Self-employed
foreach o of local O {
	rifhdreg `o'  `Controls' iCreativity iCogAbility `version'CreatOcc  [pweight=1/p_`o'] if SE==1, rif(mean) cluster(NCDSID)
	
	mat b = e(b)
	mat b = b'
	svmat b
	qui su b1 if b1!=0
	mat define p= r(N)
	drop b1
	qui estadd scalar Par=p[1,1]
	qui estadd scalar Ni=e(N_clust) 
	qui estadd local mdv "-"
	est sto es_`o'_SE
}




local text0 "prehead( & \multicolumn{3}{c}{Individuals in work}     \\ \vspace{-.25cm} \\ \cline{2-4} \\ \vspace{-.65cm}\\ ) "
local text  `text0' prefoot( \cline{2-4}  \vspace{-.3cm}\\ )  posthead(\vspace{-.3cm}\\ \cline{2-4} )  postfoot(\vspace{-.3cm} \\  ) 

* Stats
local stP "stats( N, fmt( %11.0gc %8.2f)  labels("Individual-year obs."  ))" 


* all individuals
******************************************************************************************

esttab es*_All using results/Tables/R_OLS_`lab'_All.tex, ///
replace `text' se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
keep(Creativity CogAbility iCreativity iCogAbility) refcat(Creativity ""  " " "", nolabel) ///
order(Creativity CogAbility iCreativity iCogAbility) ///
varlabels(Creativity Creativity CogAbility "Cognitive ability" iCreativity "Creativity $\times$ Creative occ." iCogAbility "Cognitive ability $\times$ Creative occ.") ///
cells(b(star fmt(3)) se(par) ".") nogaps   alignment(c) ///
mtitles("Log hourly earnings" "Log weekly earnings" "Log weekly hours" ) frag `stP'

* Employees
******************************************************************************************

esttab es*_Emp using results/Tables/R_OLS_`lab'_Emp.tex, ///
replace `text' se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
keep(Creativity CogAbility iCreativity iCogAbility) refcat(Creativity ""  " " "", nolabel) ///
order(Creativity CogAbility iCreativity iCogAbility) ///
varlabels(Creativity Creativity CogAbility "Cognitive ability" iCreativity "Creativity $\times$ Creative occ." iCogAbility "Cognitive ability $\times$ Creative occ.") ///
cells(b(star fmt(3)) se(par) ".") nogaps   alignment(c) ///
mtitles("Log hourly earnings" "Log weekly earnings" "Log weekly hours" ) frag `stP'

* Self-employeed
******************************************************************************************

esttab es*_SE using results/Tables/R_OLS_`lab'_SE.tex, ///
replace `text' se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
keep(Creativity CogAbility iCreativity iCogAbility) refcat(Creativity ""  " " "", nolabel) ///
order(Creativity CogAbility iCreativity iCogAbility) ///
varlabels(Creativity Creativity CogAbility "Cognitive ability" iCreativity "Creativity $\times$ Creative occ." iCogAbility "Cognitive ability $\times$ Creative occ.") ///
cells(b(star fmt(3)) se(par) ".") nogaps   alignment(c) ///
mtitles("Log hourly earnings" "Log weekly earnings" "Log weekly hours" ) frag `stP'








est drop _all
restore
